package code.easy.refresh.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import code.easy.refresh.demo.ui.theme.EasyRefreshTheme
import code.easy.refresh.rememberRefreshState
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            EasyRefreshTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {
    Column {
        val refreshState = rememberRefreshState()
        val scope = rememberCoroutineScope()
        MyRefresh(
            refreshState = refreshState,
            onRefresh = {
                scope.launch {
                    // loading data
                    delay(400)
                    refreshState.isRefreshing = false
                }
            },
            modifier = Modifier.fillMaxSize(),
        ) {
            LazyColumn {
                items(100) { index ->
                    Text(text = "item item item item item item item item item $index", modifier = Modifier.clickable {
                        Log.d("itemitem", "item = $index")
                    })
                }
            }
        }
    }
//    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    EasyRefreshTheme {
        Greeting("Android")
    }
}